package com.aiyige.page.login.country;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.aiyige.R;
import com.aiyige.model.Country;

import java.util.List;

/**
 * Created by gjz on 9/4/16.
 */
public class CountryAdapter extends RecyclerView.Adapter<CountryAdapter.CountrysViewHolder> {

    private List<Country> Countrys;
    private int layoutId;

    private OnItemClickListener mOnItemClickListener = null;

    public CountryAdapter(List<Country> Countrys, int layoutId) {
        this.Countrys = Countrys;
        this.layoutId = layoutId;
    }

    @Override
    public CountrysViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View view = inflater.inflate(layoutId, null);
        return new CountrysViewHolder(view);
    }

    @Override
    public void onBindViewHolder(CountrysViewHolder holder, int position) {
        final Country country = Countrys.get(position);
        holder.tvName.setText(country.getCountryName() + " +" + country.getPhoneCode());
        holder.rlItemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnItemClickListener != null) {
                    mOnItemClickListener.onItemClick(v, country);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return Countrys.size();
    }

    class CountrysViewHolder extends RecyclerView.ViewHolder {
        public TextView tvName;
        public RelativeLayout rlItemView;


        public CountrysViewHolder(View itemView) {
            super(itemView);
            rlItemView = (RelativeLayout) itemView.findViewById(R.id.rl_country_item);
            tvName = (TextView) itemView.findViewById(R.id.tv_name);

        }
    }

    public void setOnItemClickListener(OnItemClickListener itemClickListener) {
        mOnItemClickListener = itemClickListener;
    }

    public interface OnItemClickListener {
        void onItemClick(View view, Country country);
    }
}